package io.agora.agorartcengine;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;

import io.agora.processor.common.connector.SinkConnector;
import io.agora.processor.media.data.CapturedFrame;
import io.agora.processor.media.data.VideoCaptureConfigInfo;
import io.agora.processor.media.manager.AudioManager;
import io.agora.processor.media.manager.VideoManager;
import io.agora.rtc.video.VideoEncoderConfiguration;
import io.flutter.plugin.platform.PlatformView;

import static io.agora.processor.common.constant.Constant.CAMERA_FACING_FRONT;

public class AgoraRendererView implements PlatformView {
    private final GLSurfaceView mSurfaceView;
    private volatile boolean mFUInit;
    private VideoManager mVideoManager;
    private AudioManager mAudioManager;

    private VideoCaptureConfigInfo mVideoCaptureConfigInfo;
    private AgoraVideoSource mVideoSource;

    private AgoraRtcEnginePlugin mPlugin;
    private volatile boolean local;

    AgoraRendererView(Context context, GLSurfaceView surfaceView, final AgoraRtcEnginePlugin plugin, boolean local) {
        this.mSurfaceView = surfaceView;
        this.local = local;
        Log.d("是否是本地预览", "本地预览: " + local);
        if (local) {
            this.mPlugin = plugin;
            if (mVideoManager == null) {
                mVideoManager = VideoManager.createInstance(context);
            }

            mSurfaceView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                @Override
                public void onViewAttachedToWindow(View v) {
                    mVideoManager.runInRenderThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!mFUInit) {
                                mPlugin.getFURenderer().onSurfaceCreated();
                                mFUInit = true;
                            }
                        }
                    });

//                    mSurfaceView.queueEvent(new Runnable() {
//                        @Override
//                        public void run() {
//                            if (!mFUInit) {
//                                mPlugin.getFURenderer().onSurfaceCreated();
//                                mFUInit = true;
//                            }
//                        }
//                    });
                }

                @Override
                public void onViewDetachedFromWindow(View v) {
                }
            });


            if (mVideoCaptureConfigInfo == null) {
                mVideoCaptureConfigInfo = new VideoCaptureConfigInfo();
            }

            mVideoCaptureConfigInfo.setVideoCaptureWidth(1080);
            mVideoCaptureConfigInfo.setVideoCaptureHeight(2210);
            mVideoCaptureConfigInfo.setVideoCaptureFps(30);
            mVideoCaptureConfigInfo.setCameraFace(CAMERA_FACING_FRONT);
            mVideoCaptureConfigInfo.setVideoCaptureFormat(VideoCaptureConfigInfo.CaptureFormat.TEXTURE_2D);
            mVideoCaptureConfigInfo.setVideoCaptureType(VideoCaptureConfigInfo.CaptureType.TEXTURE);


            mVideoManager.allocate(mVideoCaptureConfigInfo);
            mVideoManager.setRenderView(mSurfaceView);
            mVideoManager.connectEffectHandler(plugin.getEffectHandler());

//            mVideoManager.attachToRTCEngine(AgoraRtcEnginePlugin.getRtcEngine());

            if (mVideoSource == null) {
                mVideoSource = new AgoraVideoSource(this.mVideoCaptureConfigInfo);
            }
            mVideoSource.enablePushDataForAgora(true);
            AgoraRtcEnginePlugin.getRtcEngine().setVideoSource(mVideoSource);

            mVideoManager.attachConnectorToRender(mVideoSource);

            mVideoManager.startCapture();

            AgoraRtcEnginePlugin.getRtcEngine().setClientRole(io.agora.rtc.Constants.CLIENT_ROLE_BROADCASTER);
            AgoraRtcEnginePlugin.getRtcEngine().setVideoEncoderConfiguration(new VideoEncoderConfiguration(
                    VideoEncoderConfiguration.VD_640x360,
                    VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_24, 800,
                    VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_PORTRAIT));
        }
    }

    @Override
    public View getView() {
        return mSurfaceView;
    }

    @Override
    public void dispose() {
        if (local) {
            mVideoManager.stopCapture();

            mVideoManager.runInRenderThread(new Runnable() {
                @Override
                public void run() {
                    mPlugin.getFURenderer().onSurfaceDestroyed();
                    mFUInit = false;
                }
            });
//
//            mSurfaceView.queueEvent(new Runnable() {
//                @Override
//                public void run() {
//                    mPlugin.getFURenderer().onSurfaceDestroyed();
//                    mFUInit = false;
//                }
//            });

            mVideoManager.deallocate();


        }
    }
}